﻿@using Easy.Web.CMS.Page
@model PageEntity
@{
    Script.Reqiured("validate").AtFoot();
    List<PageEntity> oldVersions = (ViewBag.OldVersions as IEnumerable<PageEntity>).ToList();
}


<div class="panel panel-default">
    <div class="panel-heading">
        <div>页面</div>
    </div>
    <div class="panel-body">
        @using (Html.BeginForm())
        {
            @Html.EditorForModel()

            <div class="toolBar">
                <input type="submit" value="保存" class="btn btn-primary" data-value="@ActionType.Update" />
                <input type="submit" value="设计" class="btn btn-success" data-value="@ActionType.Design" />
                <input type="submit" value="发布" class="btn btn-info publish" data-value="@((int) ActionType.Publish)" />
                <input type="submit" value="删除" class="btn btn-danger delete" data-value="@ActionType.Delete" />
                <input type="button" class="btn btn-default cancel" value="取消" />
            </div>
        }
    </div>
</div>

<div id="versions" class="form-group">
    <label class="col-sm-2 control-label">历史版本</label>
    <div class="col-sm-10">
        @if (oldVersions != null)
        {
            int index = 0;
            <table class="table table-striped table-bordered table-hover old-versions">
                <thead>
                    <tr>
                        <th width="55">
                            版本
                        </th>
                        <th width="100">操作</th>
                        <th>发布日期</th>
                        <th>描述</th>
                        <th>发布人</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var page in oldVersions)
                    {
                        bool isCurrent = (index + 1) == oldVersions.Count;
                        <tr>
                            <td>
                                
                                @if (isCurrent)
                                {
                                    <span class="badge">当前</span>
                                }
                                else
                                {
                                    @(index + 1)
                                }
                            </td>
                            <td>
                                <a href="@Url.Action("ViewPage", new {ID = page.ID})" class="glyphicon glyphicon-eye-open" title="查看" target="_blank"></a>
                                @if (!isCurrent)
                                {
                                    <a href="javascript:void(0)" class="glyphicon glyphicon-share-alt revert" title="撤回到该版本" data-id="@page.ID"></a>
                                    <a href="javascript:void(0)" class="glyphicon glyphicon-remove remove" title="删除该版本" data-id="@page.ID"></a>
                                }
                            </td>
                            <td>
                                @(page.PublishDate.HasValue ? page.PublishDate.Value.ToString("f") : "")
                            </td>
                            <td>
                                @page.Description
                            </td>
                            <td>
                                @page.CreatebyName
                            </td>
                        </tr>
                                index++;
                    }
                </tbody>
            </table>
        }
    </div>
</div>

@using (Script.AtFoot())
{
    <script type="text/javascript">
        $(function () {
            var url = $("#Url");
            url.data("url", url.val().substring(0, url.val().lastIndexOf("/") + 1));
            $(".delete").click(function () {
                return confirm("删除页面会删除对应的子页面，确定要删除吗？");
            });
            $("#PageUrl").blur(function () {
                var url = $("#Url");
                var parentUrl = url.data("url");
                if (!parentUrl) {
                    parentUrl = "~/";
                }
                var thisUrl = $(this).val();
                var replaceValue = ["/", " ", "?", "`", "~", "!", "@@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", "[", "]", "{", "}", "\\", "|", ";", ":", "'", "\"", ",", ".", "<", ">"];
                for (var i = 0; i < replaceValue.length; i++) {
                    thisUrl = thisUrl.replaceAll(replaceValue[i], "_");
                }
                url.val(parentUrl + thisUrl);
            });
            var retainLatest = true;
            $(document).on("click", ".revert", function () {
                var id = $(this).data("id");
                Easy.ShowMessageBox("提示", '确定要撤回到该版本吗？<br/><div class="checkbox"><label><input class="retain-latest" type="checkbox" checked> 保留当前的修改</label></div>', function () {
                    $.post("@Url.Action("Revert")", { ID: id, RetainLatest: retainLatest }, function (data) {
                        if (data.Status == 1) {
                            window.location.reload();
                        } else {
                            Easy.ShowMessageBox("提示", data.Message);
                        }
                    }, "json");
                }, true);
            });
            $(document).on("click", ".remove", function () {
                var id = $(this).data("id");
                Easy.ShowMessageBox("提示", "确定要删除该版本吗？", function () {
                    $.post("@Url.Action("DeleteVersion")", { ID: id }, function (data) {
                        if (data.Status == 1) {
                            window.location.reload();
                        } else {
                            Easy.ShowMessageBox("提示", data.Message);
                        }
                    }, "json");
                }, true);
            });
            $(document).on("click", ".retain-latest", function() {
                retainLatest = $(this).prop("checked");
            });
            $("#versions").appendTo($(".form-horizontal"));
        });

    </script>

}